package com.yxzx.ebs.handler;

import com.yxzx.common.EduException;
import com.yxzx.common.ExceptionUtil;
import com.yxzx.common.YxzxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public YxzxResult error(Exception e){
        e.printStackTrace();
        return YxzxResult.error().message("出现异常");
    }

    @ResponseBody
    @ExceptionHandler(ArithmeticException.class)
    public YxzxResult error(ArithmeticException e){
        e.printStackTrace();
        return YxzxResult.error().message("出现异常，0不能为除数。");
    }

    @ResponseBody
    @ExceptionHandler(EduException.class)
    public YxzxResult error(EduException e){
        e.printStackTrace();
        log.error("出现异常: "+ ExceptionUtil.getMessage(e));
        return YxzxResult.error().message(e.getMessage()).code(e.getCode());
    }
}
